<template>
	<a-card :bordered="false">
		<div class="table-page-search-wrapper">
			<a-form layout="inline">
				<a-row :gutter="48">

					<a-col :md="8" :sm="24">
						<a-form-item label="生产单号">
							<a-input v-model="queryParam.orderNumber" placeholder="请输入生产单号" />
						</a-form-item>
					</a-col>
					<a-col :md="24" :sm="24" v-if="canReadOrgIdCompany()">
						<a-form-item label="归属公司：">
							<FCOrgRadioGroup v-model="queryParam.orgId" :filter-types="[$businessEnum.ORG_TYPE_ENUM.COMPANY.id]" />
						</a-form-item>
					</a-col>
					<a-col :md="7" :sm="6">
						<a-form-item label="物料编号">
							<a-input v-model="queryParam.jzMaterialCode" placeholder="请输入物料编号" />
						</a-form-item>
					</a-col>
					<el-col :md="7" :sm="6">
						<a-form-item label="状 态">
							<a-radio-group default-value="" button-style="solid" v-model="queryParam.scckStatus">
								<a-radio-button value="">全部</a-radio-button>
								<a-radio-button value="1">待出库</a-radio-button>
								<a-radio-button value="2">出库中</a-radio-button>
								<a-radio-button value="3">己出库</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</el-col>
					<a-col :md="7" :sm="6">
						<a-form-item label="领用方">
							<a-select placeholder="全部" v-model="queryParam.classGroupId">
								<!-- <a-select-option :value="null">
									全部
								</a-select-option> -->
								<a-select-option :values="item.classGroupId" v-for="item in teamList" :key="item.classGroupId">
									{{ item.name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :md="!advanced && 8 || 24" :sm="24">
						<span class="table-page-search-submitButtons"
							:style="advanced && { float: 'right', overflow: 'hidden' } || {}">
							<a-button type="primary" @click="table.refresh(true)">查询</a-button>
							<a-button style="margin-left: 8px"
								@click="() => { queryParam.orderNumber = null; queryParam.orgId = null; queryParam.jzMaterialCode = null; queryParam.classGroupId = null; table.refresh(true) }">重置</a-button>
						</span>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div style="margin: 10px 0px;">
			<Export text="出库单导出" v-if="hasBtnPremission(['emsOrder:scdckOrderExport'])" title="出库单导出"
				url="/emsOrder/scdckOrderExport" :isBody="true" :obj="{
					...queryParam,
				}" />
		</div>
		<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true"
			:rowKey="(record) => record.code">
			<span slot="scckStatus" slot-scope="text, record">
				<a-tag color="#2db7f5" v-if="record.scckStatus == 1"> 待出库 </a-tag>
				<a-tag color="#108ee9" v-if="record.scckStatus == 2"> 出库中 </a-tag>
				<a-tag color="#87d068" v-if="record.scckStatus == 3"> 己出库 </a-tag>
			</span>
			<span slot="isExtraCk" slot-scope="text, record">
				<span v-if="record.isExtraCk == 1">否</span>
				<span v-if="record.isExtraCk == 2">是</span>
			</span>
			<span slot="action" slot-scope="text, record">
				<a @click="outStoreAddForm.show(record)">出库</a>
				<a-divider type="vertical" />
				<a @click="outStoreDetailForm.show(record)">出库详情</a>
				<a-divider type="vertical" v-if="hasPerm('emsPlanOrder:add')" />
				<a @click="backStore.show(record)" v-if="hasPerm('emsPlanOrder:add')">退库</a>
				<a-divider type="vertical" />
				<a @click="backStoreRecordDetailModal.show(record)">退库入库单明细</a>
				<a-divider type="vertical" />
				<a @click="$refs.editRemark.show(record, materialType)">备注</a>
			</span>

		</s-table>


		<!--  生产单出库操作页  -->
		<out-store-add-form ref="outStoreAddForm" @ok="handleResetData" :material-type="materialType" />
		<out-store-detail-form ref="outStoreDetailForm" :material-type="materialType" />
		<back-store ref="backStore" />
		<back-store-record-detail-modal ref="backStoreRecordDetailModal" />
		<edit-remark ref="editRemark" @ok="table.refresh(true)" />
	</a-card>
</template>

<script lang="ts">
import { Component, Vue, Ref, Watch } from "vue-property-decorator";
import { STable } from '@/components'
import { page } from '@/api/modular/main/emsorder/emsOrderManage'
import FCOrgRadioGroup from '@/components/FcOrgRadioGroup/FCOrgRadioGroup.vue'
import OutStoreAddForm from './outStoreAddForm.vue'
import OutStoreDetailForm from './outStoreDetailForm.vue'
import BackStore from './backStore.vue'
import BackStoreRecordDetailModal from './backStoreRecordDetailModal.vue'

import EditRemark from "./editRemark.vue";

import { page as teamPage } from '@/api/modular/main/classGroup/classGroup'

@Component({
	components: {
		STable,
		OutStoreAddForm,
		OutStoreDetailForm,
		FCOrgRadioGroup,
		BackStoreRecordDetailModal,
		BackStore,
		EditRemark
	}
})

export default class ProductionOrderByWaitOutStore extends Vue {
	@Ref() readonly table!: any
	@Ref() readonly outStoreAddForm!: OutStoreAddForm
	@Ref() readonly outStoreDetailForm!: OutStoreDetailForm
	@Ref() readonly backStoreRecordDetailModal!: BackStoreRecordDetailModal
	@Ref() readonly backStore!: BackStore


	advanced = false // 高级搜索 展开/关闭
	queryParam = {
		orderNumber: '',
		orgId: '',
		orderType: '',
		scckStatus: '',
		executeStatus: '',
		materialType: '',
		classGroupId: undefined,
		jzMaterialCode: '',
		scMaterialType: null
	}
	teamList = []

	columns = []

	columnsType = ''
	suppliers = []
	orgs = []
	viewState = ''

	@Watch('$route.path')
	watchRoutePath(newValue: string) {
		console.log('newValue', newValue)
		setTimeout(
			() => {
				this.handleResetData()
			}, 100)
	}


	get routePath() {
		let urlParam = this.$util.getUrlParam(this.$route.path)
		return urlParam
	}

	get materialType() {
		return this.routePath?.materialType || this.$businessEnum.MATERIAL_TYPE_ENUM.material.id
	}

	created() {
		this.getTeamList();
		this.columns = this.getColumns();
		console.log(this.getColumns())
	}

	// 加载数据方法 必须为 Promise 对象
	loadData(parameter) {
		/*
		生产单待出库查询的是进行中的生产单.只要生产单还在进行中,就一直可以出库.
		 */
		this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.produceOrder.id
		this.queryParam.executeStatus = this.$businessEnum.EXECUTE_STATUS_ENUM.IN_PROGRESS.id
		this.queryParam.materialType = this.materialType
		this.queryParam.scMaterialType = this.materialType
		return page(Object.assign(parameter, this.queryParam)).then((res) => {
			return res.data
		})
	}
	getColumns() {
		let columns = [];
		if (this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.material.id) {//原料
			columns = [
				{
					align: 'center',
					title: '序号', width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					align: 'center',
					title: '生产单号',
					dataIndex: 'orderNumber'
				},
				{
					align: 'center',
					title: '状态',
					dataIndex: 'scckStatus',
					scopedSlots: { customRender: 'scckStatus' }
				},
				{
					align: 'center',
					title: '是否额外出单',
					dataIndex: 'isExtraCk',
					scopedSlots: { customRender: 'isExtraCk' }
				},
				{
					align: 'center',
					title: '备注',
					dataIndex: 'ylScckRemark',
					ellipsis: true
				},
				{
					align: 'center',
					title: '开始时间',
					dataIndex: 'createTime'
				},
				{
					align: 'center',
					title: '操作',
					dataIndex: '',
					width: '360px',
					scopedSlots: { customRender: 'action' }
				}
			]
		} else if (this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.customized.id) {//定制料
			columns = [
				{
					align: 'center',
					title: '序号', width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					align: 'center',
					title: '生产单号',
					dataIndex: 'orderNumber'
				},
				{
					align: 'center',
					title: '状态',
					dataIndex: 'scckStatus',
					scopedSlots: { customRender: 'scckStatus' }
				},
				{
					align: 'center',
					title: '是否额外出单',
					dataIndex: 'isExtraCk',
					scopedSlots: { customRender: 'isExtraCk' }
				},
				{
					align: 'center',
					title: '备注',
					dataIndex: 'dzlScckRemark',
					ellipsis: true
				},
				{
					align: 'center',
					title: '开始时间',
					dataIndex: 'createTime'
				},
				{
					align: 'center',
					title: '操作',
					dataIndex: '',
					width: '360px',
					scopedSlots: { customRender: 'action' }
				}
			]
		} else if (this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.cable.id) {//线缆
			columns = [
				{
					align: 'center',
					title: '序号', width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					align: 'center',
					title: '生产单号',
					dataIndex: 'orderNumber'
				},
				{
					align: 'center',
					title: '状态',
					dataIndex: 'scckStatus',
					scopedSlots: { customRender: 'scckStatus' }
				},
				{
					align: 'center',
					title: '是否额外出单',
					dataIndex: 'isExtraCk',
					scopedSlots: { customRender: 'isExtraCk' }
				},
				{
					align: 'center',
					title: '备注',
					dataIndex: 'xlScckRemark',
					ellipsis: true
				},
				{
					align: 'center',
					title: '开始时间',
					dataIndex: 'createTime'
				},
				{
					align: 'center',
					title: '操作',
					dataIndex: '',
					width: '360px',
					scopedSlots: { customRender: 'action' }
				}
			]
		} else {
			columns = [];
		}
		return columns;
	}
	toggleAdvanced() {
		this.advanced = !this.advanced
	}
	//获取领用方
	getTeamList() {
		teamPage({
			pageSize: -1,
			status: 0
		}).then(res => {
			this.teamList = res.data.rows;
		})
	}
	handleResetData() {
		this.table.refresh()
		this.table.clearSelected()
	}


	canReadOrgIdCompany() {
		return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id) && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
	}

}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
